<template>
  <div>
    <div class="img">
      <img :src="robotTypeImg" alt="" />
      <div class="battery-bgi" v-if="gdType">
        <div class="battery-bgi-title">
          <div></div>
          <div>电池</div>
        </div>
        <div class="battery-bgi-info">
          <el-tooltip
            class="item"
            effect="dark"
            content="电池电压"
            placement="right"
          >
            <div>{{ Number(eletricInfo.voltage) / (1000).toFixed(2) }}V</div>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="电池电流"
            placement="right"
          >
            <div>
              {{ Number(eletricInfo.current) / (1000).toFixed(2) }}A/{{
                eletricInfo.current > 0 ? "充电" : "放电"
              }}
            </div>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="电池温度"
            placement="right"
          >
            <div>{{ eletricInfo.temp }}°C</div>
          </el-tooltip>
        </div>
      </div>
    </div>
    <div class="task">
      <div class="task-title">
        <span>任务下发</span>
        <span @click="taskImmediteClick">立即执行&nbsp;&nbsp;></span>
      </div>
      <div class="task-options">
        <el-select
          v-model="tempId"
          filterable
          remote
          reserve-keyword
          clearable
          placeholder="请输入关键词"
        >
          <el-option
            v-for="item in templateList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
    </div>
    <!-- 立即执行弹框 -->
    <TaskDialog
      :taskDialog="taskVisible"
      :taskName="globalCurrentTaskInfo.taskName"
      @taskDialogConfirm="dialogConfirm"
      @taskDialogCancle="dialogCancle"
    ></TaskDialog>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import TaskDialog from "@/components/TaskDialog";
import {
  API_topInfo, //头部信息
} from "@/api/robot/monitor";
import { API_TemplateList, API_immediate } from "@/api/task/template";
import { API_ImmediateRun } from "@/api/task/manage";
import {
  API_taskControl, //任务控制
  API_batteryInfo,
} from "@/api/robot/monitor";
export default {
  components: {
    TaskDialog,
  },
  props: {
    gdType: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      taskVisible: false, //立即执行弹框
      robotTypeImg: "",
      tempId: null,
      templateList: [], //模板数据
      bateryTimer: null,
      eletricInfo: {},
    };
  },
  computed: {
    ...mapGetters(["globalCurrentTaskInfo"]),
  },
  created() {
    this.getRobotType(); //不同机型展是图片
    this.getTemplateList(); //任务模板列表
  },
  mounted() {
    this.bateryTimer = setInterval(() => {
      this.getBateryInfo();
    }, 10000);
  },
  methods: {
    /**电流 */
    getBateryInfo() {
      API_batteryInfo().then((res) => {
        if (res.code == 200) {
          console.warn("电流", res);
          this.eletricInfo = JSON.parse(res.data.batteryStatus);
        }
      });
    },
    dialogCancle() {
      this.taskVisible = false;
    },
    /**立即执行操作 */
    taskImmediteClick() {
      if (!this.tempId) {
        this.$message.warning("请先选择下发执行命令的模板！");
        return;
      }
      // 判断是否有正在执行的任务
      if (
        this.globalCurrentTaskInfo.taskId != -1 &&
        this.globalCurrentTaskInfo.taskProcess == "3"
      ) {
        //展示弹框
        this.taskVisible = true;
      } else {
        this.$confirm("确认要立即执行？ 是否继续?", "执行确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let immediatePara = {
              id: this.tempId,
              name: "立即执行" + this.insertTempName(),
              agingTime: "1",
            };
            API_immediate(immediatePara).then((res) => {
              if (res.code == 200) {
                this.$message.success("立即执行成功！");
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消",
            });
          });
      }
    },
    /**立即执行弹框内部确认 */
    dialogConfirm(radioVal) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      if (radioVal == 1) {
        //暂停当前任务
        API_taskControl({
          cmd: "3X12",
          values: [
            this.globalCurrentTaskInfo.taskId,
            this.globalCurrentTaskInfo.execId,
          ],
        })
          .then((res) => {
            if (res.code == 200 && res.data.nerrcode == 0) {
              this.$message({
                type: "success",
                message: "任务已暂停!",
              });
              //执行当前任务
              API_ImmediateRun(this.taskId).then((res) => {
                if (res.code == 200) {
                  this.msgSuccess("立即执行成功！");
                }
              });
            } else {
              this.msgError("暂停失败，请重试！");
            }
          })
          .finally(() => {
            loading.close();
            this.taskVisible = false;
          });
      } else if (radioVal == 2) {
        //终止当前任务
        API_taskControl({
          cmd: "3X13",
          values: [
            this.globalCurrentTaskInfo.taskId,
            this.globalCurrentTaskInfo.execId,
          ],
        })
          .then((res) => {
            if (res.code == 200 && res.data.nerrcode == 0) {
              this.$message({
                type: "success",
                message: "任务已终止!",
              });
              //执行当前任务
              API_ImmediateRun(this.taskId).then((res) => {
                if (res.code == 200) {
                  this.msgSuccess("立即执行成功！");
                }
              });
            } else {
              this.msgError("终止失败，请重试！");
            }
          })
          .finally(() => {
            loading.close();
            this.taskVisible = false;
          });
      } else {
        //继续当前任务
      }
    },
    /**获取机器人机型 展示不同的图片 */
    getRobotType() {
      API_topInfo().then((res) => {
        if (res.code == 200) {
          let robotType = res.data.robotType;
          robotType = robotType.slice(0, robotType.indexOf("-") + 2);
          let robotTypeEnum = [
            "T3C",
            "T3S",
            "T3J",
            "T3F",
            "T5E",
            "T7E",
            "T7S",
            "T7C",
            "T7A",
            "T9W",
            "T9C",
            "T9R",
          ];
          let handleRobotType = robotType.replace("-", "");
          if (robotTypeEnum.indexOf(handleRobotType) != -1) {
            this.robotTypeImg = require(`@/assets/images/robottype/${handleRobotType}.png`);
          } else {
            this.robotTypeImg = require(`@/assets/images/robottype/default.png`);
          }
        }
      });
    },
    /**任务模板列表 */
    getTemplateList() {
      API_TemplateList({}).then((res) => {
        if (res.code == 200) {
          if (res.rows.length > 0) {
            this.templateList = res.rows;
          } else {
            this.templateList = [];
          }
        }
      });
    },
    /**获取本地时间： */
    insertTempName() {
      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth();
      month = month + 1;
      var date = now.getDate();
      var hours = now.getHours();
      var minuts = now.getMinutes();
      var seconds = now.getSeconds();
      if (month < 10) {
        month = "0" + month;
      }
      if (date < 10) {
        date = "0" + date;
      }
      var time =
        year.toString() +
        month.toString() +
        date.toString() +
        hours.toString() +
        minuts.toString() +
        seconds.toString();
      return time;
    },
  },
  beforeDestroy() {
    this.bateryTimer = null;
    clearInterval(this.bateryTimer);
  },
};
</script>
<style lang="scss" scoped>
.img {
  flex: 1;
  display: flex;
  justify-content: center;
  padding-bottom: 3px;
  border-bottom: 1px dashed rgba(204, 246, 255, 0.2);
  img {
    width: 100px;
    height: 78px;
  }
}
.battery-bgi {
  display: flex;
  width: 112px;
  height: 72px;
  padding: 6px;
  box-sizing: border-box;
  background: url("../../../../assets/images/home/batterybgi.png") no-repeat
    center center;
  background-size: 100% 100%;
  &-title {
    div:nth-child(1) {
      width: 12px;
      height: 4px;
      margin-left: 5px;
      background: #263440;
      border-radius: 2px 2px 0px 0px;
    }
    div:nth-child(2) {
      display: flex;
      width: 22px;
      height: 55px;
      align-items: center;
      margin-top: 1px;
      text-align: center;
      color: #a6acb1;
      font-size: 12px;
      background: #263440;
      border-radius: 2px 2px 0px 0px;
    }
  }
  &-info {
    display: flex;
    flex-direction: column;
    margin-left: 10px;
    font-size: 12px;
    color: #ffffff;
    line-height: 12px;
    div {
      flex: 1;
      display: flex;
      align-items: center;
    }
  }
}
// 任务区域
.task {
  padding: 15px;
  box-sizing: border-box;
  &-title {
    display: flex;
    justify-content: space-between;
    font-family: SourceHanSansSC-Bold, SourceHanSansSC;
    span:nth-child(1) {
      font-size: 14px;
      font-weight: bold;
      color: #ffffff;
      line-height: 14px;
    }
    span:nth-child(2) {
      font-size: 12px;
      font-weight: 400;
      color: #00d1ff;
      line-height: 12px;
      cursor: pointer;
    }
  }
  &-options {
    margin-top: 13px;
  }
}
</style>
